package com.feidian.helper.ui.view;

import android.app.Activity;
import android.app.LoaderManager;
import android.content.Context;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;

import com.feidian.helper.R;
import com.feidian.helper.provider.ZkgjProvider;

public class ProfessionSpinner extends Spinner {
	
	private static final int LOADER_PROFESSION = 3;

	private Activity mContext;

	private SimpleCursorAdapter mProfessionAdapter;;

	public ProfessionSpinner(Context context, AttributeSet attrs, int defStyle,
			int mode) {
		super(context, attrs, defStyle, mode);
		mContext = (Activity) context;
		initView();
	}

	public ProfessionSpinner(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = (Activity) context;
		initView();
	}

	public ProfessionSpinner(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = (Activity) context;
		initView();
	}

	public ProfessionSpinner(Context context) {
		super(context);
		mContext = (Activity) context;
		initView();
	}
	
	private void initView(){
		mProfessionAdapter = new SimpleCursorAdapter(mContext,
				R.layout.spinner_text_view, null,
				new String[] { ZkgjProvider.Profession.NAME },
				new int[] { android.R.id.text1 }, 0);
		mProfessionAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		this.setAdapter(mProfessionAdapter);
	}
	
	public void start(Bundle args) {
		mContext.getLoaderManager().restartLoader(LOADER_PROFESSION, args,
				mCursorCallback);
	}
	
	private LoaderManager.LoaderCallbacks<Cursor> mCursorCallback = new LoaderManager.LoaderCallbacks<Cursor>() {

		@Override
		public Loader<Cursor> onCreateLoader(int id, Bundle args) {
			switch (id) {
			case LOADER_PROFESSION: {
				String mTypeFilter = args.getString("degree");
				String mCollegeFilter = args.getString("college");
				String[] projection = new String[] {
						ZkgjProvider.CollegeProfession.TABLE_NAME + "."
								+ ZkgjProvider.CollegeProfession._ID,
						ZkgjProvider.CollegeProfession.COLLEGE,
						ZkgjProvider.CollegeProfession.PROFESSION,
						ZkgjProvider.Profession.TABLE_NAME + "."
								+ ZkgjProvider.Profession.NAME,
						ZkgjProvider.Profession.DEGREE_TYPE, };
				if (!TextUtils.isEmpty(mTypeFilter)
						&& !TextUtils.isEmpty(mCollegeFilter)) {
					return new CursorLoader(mContext,
							ZkgjProvider.CollegeProfession.CONTENT_URI,
							projection, ZkgjProvider.CollegeProfession.COLLEGE
									+ "=? AND "
									+ ZkgjProvider.Profession.DEGREE_TYPE
									+ "=?", new String[] { mCollegeFilter,
									mTypeFilter },
							ZkgjProvider.CollegeProfession.TABLE_NAME + "."
									+ ZkgjProvider.CollegeProfession._ID);
				}else if(TextUtils.isEmpty(mTypeFilter)
						&& !TextUtils.isEmpty(mCollegeFilter)) {
					return new CursorLoader(mContext,
							ZkgjProvider.CollegeProfession.CONTENT_URI,
							projection, ZkgjProvider.CollegeProfession.COLLEGE
									+ "=?", new String[] { mCollegeFilter },
							ZkgjProvider.CollegeProfession.TABLE_NAME + "."
									+ ZkgjProvider.CollegeProfession._ID);

				}


			}
			default:
				throw new RuntimeException(
						"the onCreateLoader loader id is not defined!");
			}

		}

		@Override
		public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
			int id = loader.getId();
			switch (id) {
			case LOADER_PROFESSION:
				mProfessionAdapter.swapCursor(data);
				break;
			default:
				throw new RuntimeException(
						"the onLoadFinished loader id is not defined!");
			}

		}

		@Override
		public void onLoaderReset(Loader<Cursor> loader) {
			int id = loader.getId();
			switch (id) {
			case LOADER_PROFESSION:
				mProfessionAdapter.swapCursor(null);
				break;
			default:
				throw new RuntimeException(
						"the onLoaderReset loader id is not defined!");
			}
		}

	};
}
